package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.ServiceItemDto;
import com.csun.cmny.provider.model.domain.OmcServiceItem;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcServiceItemService;
import com.csun.cmny.provider.validator.OmcServiceItemAddValidator;
import com.csun.cmny.provider.validator.OmcServiceItemEditValidator;
import com.csun.cmny.provider.vo.ServiceItemVo;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: xcr
 * @Date: 2019-05-25
 * @Description: the class omc ServiceItem controller
 */
@RestController
@RequestMapping(value = "/serviceItem", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcServiceItemController extends BaseController {

    @Resource
    OmcServiceItemService omcServiceItemService;

    @Resource
    CmnyMessageSource cmnyMessageSource;

    /*
    * 添加服务项
    * */
    @ValidateAnnotation
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/add")
    public Wrapper addService(@Validated(OmcServiceItemAddValidator.class) @RequestBody List<ServiceItemDto> serviceItemDtos,
                              BindingResult br) {

        Integer result = omcServiceItemService.saveServiceItem(serviceItemDtos, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /*
    * 修改服务项
    * */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/edit")
    public Wrapper editService(@Validated(OmcServiceItemEditValidator.class) @RequestBody ServiceItemDto serviceItemDto,
                               BindingResult br){
        OmcServiceItem omcServiceItem = new OmcServiceItem();
        BeanUtils.copyProperties(serviceItemDto,omcServiceItem);
        Integer result = omcServiceItemService.updateByPrimarykey(omcServiceItem);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /*
    * 删除服务项
    * */
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/deleteById/{serviceItemId}")
    public Wrapper deleteService(@PathVariable Long serviceItemId) {

        // 校验serviceItemId是否为空
        Preconditions.checkArgument(serviceItemId != null, cmnyMessageSource
                .getMessage("serviceItem.id.null", null));

        Integer result = omcServiceItemService.deleteServiceItemAndReportItem(serviceItemId);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);

    }

    /*
    * 通过ServiceId查找服务项
    * */
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/queryListByServiceId/{serviceId}")
    public Wrapper queryServiceItemList(@PathVariable Long serviceId) {

        List<ServiceItemVo> serviceItems = omcServiceItemService.selectByServiceId(serviceId);

        //return WrapMapper.ok(serviceItems);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),serviceItems);
    }

//    public Integer saveServiceItem(List<ServiceItemDto> serviceItemDtos) {
//        OmcServiceItem omcServiceItem = new OmcServiceItem();
//        List<OmcServiceItem> omcServiceItems = new ArrayList<>();
//        for(ServiceItemDto dto : serviceItemDtos){
//            BeanUtils.copyProperties(dto, omcServiceItem);
//            omcServiceItems.add(omcServiceItem);
//        }
//        return omcServiceItemService.saveServiceItem(omcServiceItems);
//    }

/*    @RequestMapping("/test")
    public Wrapper testServiceParam() {

        List<ReportItemDto> reportItemDtos = Lists.newArrayList();
        for (int i = 0; i < 2; i++) {
            ReportItemDto reportItemDto = new ReportItemDto();
            reportItemDto.setId((long)i);
            reportItemDto.setReportItemContent(i + "_content");
            reportItemDto.setReportOrder(i);
            reportItemDto.setServiceItemId((long)i);

            reportItemDtos.add(reportItemDto);
        }

        ServiceItemDto serviceItemDto = new ServiceItemDto();
        serviceItemDto.setId(1l);
        serviceItemDto.setServiceId(1l);
        serviceItemDto.setItemName("test");
        serviceItemDto.setItemOrder(1);
        serviceItemDto.setItemType(1);
        serviceItemDto.setReportItemDtos(reportItemDtos);

        return WrapMapper.ok(serviceItemDto);
    }*/


    @RequestMapping("/test")
    public Wrapper test() {

        List<OmcServiceItem> omcServiceItems = Lists.newArrayList();
        for (int i = 0; i < 2; i++) {
            OmcServiceItem omcServiceItem = new OmcServiceItem();
            omcServiceItem.setItemOrder(i);
            omcServiceItem.setServiceId((long)i);

            omcServiceItems.add(omcServiceItem);
        }

        int result = omcServiceItemService.batchSave(omcServiceItems);

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

}
